<template>
  <div>
    <div>【大区数据信息】</div>
    <div ref="horizontalRef" class="w-full h-full"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
    data: {
        type: Object,
        required: true
    }
})

const horizontalRef = ref(null)
let myChart: any = null
onMounted(() => {
    myChart = echarts.init(horizontalRef.value)
    renderChart()
})

const renderChart = () => {
    let option = {
        xAxis: {
            show: false,
            type: 'value',
            max: function(value: any) {
                return Math.floor(value.max * 1.2)
            }
        },
        yAxis: {
            type: 'category',
            data: props.data.regions.map((item: any) => item.name),
            inverse: true,
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                show: true,
                color: '#9EB1C8'
            }
        },
        grid: {
            top: 0,
            right: 0,
            bottom: 0,
            left: 0,
            containLabel: true
        },
        series: [
            {
                type: 'bar',
                showBackground: true,
                backgroundStyle: {
                    color: 'rgba(180, 180, 180, 0.2)',
                },
                data: props.data.regions.map((item: any) => {
                    return {
                        name: item.name,
                        value: item.value
                    }
                }),
                itemStyle: {
                    color: '#479AD3',
                    borderRadius : 5,
                    shadowColor: 'rgba(0, 0, 0, 0.3)',
                    shadowBlur: 5
                },
                barWidth: 12,
                label: {
                    show: true,
                    position: 'right',
                    color: '#fff'
                }
            }
        ]

    }
    myChart.setOption(option)
}

watch(()=> props.data, ()=>{
    renderChart()
})

</script>

<style scoped>
</style>
